package com.woniu.his.bms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.his.bms.common.BmsResultCode;
import com.woniu.his.bms.common.enums.BmsStatusEnums;
import com.woniu.his.bms.infra.model.SettlementCat;
import com.woniu.his.bms.infra.mapper.SettlementCatMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.his.bms.service.SettlementCatService;
import com.woniu.his.bms.service.dto.PageSettlementCatDto;
import com.woniu.his.bms.service.dto.SettlementCatDto;
import com.woniu.his.bms.service.param.FindSettlementCatServiceParam;
import com.woniu.his.bms.service.param.UpdateSettlementCatServiceParam;
import com.woniu.his.exception.HisException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 结算类别 服务实现类
 * </p>
 *
 * @author chenbo
 * @since 2022-11-29
 */
@Service
public class SettlementCatServiceImpl extends ServiceImpl<SettlementCatMapper, SettlementCat> implements SettlementCatService {

    @Resource
    private Snowflake snowflake;

    @Override
    public void addSettlementCat(String name) {

        //判断：新添加的结算类别不能重名
        QueryWrapper<SettlementCat> wrapper=new QueryWrapper<>();
        wrapper.eq("name",name).eq("status", BmsStatusEnums.ON.getCode());
        SettlementCat one = getOne(wrapper);
        if (one!=null) throw new HisException(BmsResultCode.SETTLEMENT_CAT_IS_EXIST);

        //添加
        SettlementCat settlementCat=new SettlementCat();
        settlementCat.setName(name);
        settlementCat.setStatus(BmsStatusEnums.ON.getCode());
        settlementCat.setCode("settlementCat"+snowflake.nextId());
        save(settlementCat);

    }

    @Override
    public void deleteSettlementCat(List<Long> ids) {

        removeByIds(ids);
    }

    @Override
    public PageSettlementCatDto findDept(FindSettlementCatServiceParam serviceParam) {

        Long current=serviceParam.getCurrent()==null ? 1 : serviceParam.getCurrent();
        Long size=serviceParam.getSize()==null ? 1 : serviceParam.getSize();

        QueryWrapper<SettlementCat> wrapper=new QueryWrapper<>();
        //条件0：选择有效的
        wrapper.eq("status",BmsStatusEnums.ON.getCode());
        //条件1：判断关键字
        if (serviceParam.getKeyword()!=null&&serviceParam.getKeyword().trim().length()>0){
            wrapper.and(w->{
                w.like("name",serviceParam.getKeyword()).or().like("code",serviceParam.getKeyword());
            });
        }
        //最终查询数据
        Page<SettlementCat> page = page(new Page<>(current,size), wrapper);
        if(page.getRecords().size()<=0) return null;
        PageSettlementCatDto result=new PageSettlementCatDto();
        List<SettlementCatDto> list= BeanUtil.copyToList(page.getRecords(),SettlementCatDto.class);

        result.setCurrent(page.getCurrent());
        result.setSize(page.getSize());
        result.setTotal(page.getTotal());
        result.setSettlementCatDtoList(list);

        return result;
    }

    @Override
    public List<SettlementCatDto> queryAllSettlementCat() {

        List<SettlementCat> list = list();
        List<SettlementCatDto> result = BeanUtil.copyToList(list,SettlementCatDto.class);
        return result;
    }

    @Override
    public void updateSettlementCat(UpdateSettlementCatServiceParam serviceParam) {

        SettlementCat one = getById(serviceParam.getId());
        if (one==null) throw new HisException(BmsResultCode.SETTLEMENT_CAT_NOT_EXIST);
        //判断：更新的结算类别不能重名
        QueryWrapper<SettlementCat> wrapper=new QueryWrapper<>();
        wrapper.eq("name",serviceParam.getName()).eq("status", BmsStatusEnums.ON.getCode());
        SettlementCat settlementCat = getOne(wrapper);
        if (settlementCat!=null) throw new HisException(BmsResultCode.SETTLEMENT_CAT_IS_EXIST);

        //更新
        one.setName(serviceParam.getName());
        updateById(one);

    }
}
